package ai.assist.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther: hui
 * @Date: 2019/3/5 14:55
 * @Description: 密码最低要求8字符
 * 最少符合下列四项中三项规则:
 * 大写英文字符
 * 小写英文字符
 * 数字字符
 * 符号字符
 */
public class CheckPasswordMeterUtils {

    public static boolean CheckPswMeter(String psw) {
        int upper = 0;
        int lower = 0;
        int chars = 0;
        int num = 0;
        //大写字母
        String upperReg = "[A-Z]";
        //小写字母
        String lowerReg = "[a-z]";
        //数字
        String numReg = "[0-9]";
        //字符
        String charReg = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
        String psd = psw.replaceAll("\\s", "");
        int length = psd.length();
        if (length < 8 || length > 20) {
            return false;
        }
        Pattern upperPattern = Pattern.compile(upperReg);
        Pattern lowerPattern = Pattern.compile(lowerReg);
        Pattern numPattern = Pattern.compile(numReg);
        Pattern charPattern = Pattern.compile(charReg);
        Matcher upperMatcher = upperPattern.matcher(psd);
        Matcher lowerMatcher = lowerPattern.matcher(psd);
        Matcher numMatcher = numPattern.matcher(psd);
        Matcher charMatcher = charPattern.matcher(psd);
        if (upperMatcher.find()) {
            upper = 1;
        }
        if (lowerMatcher.find()) {
            lower = 1;
        }
        if (numMatcher.find()) {
            num = 1;
        }
        if (charMatcher.find()) {
            chars = 1;
        }
        int result = upper + lower + num + chars;
        if (result < 3) {
            return false;
        }
        return true;
    }

}
